package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"web-server/config"
	"web-server/pkg/log"
)

func GetDB() *gorm.DB {
	var mysqlDB *gorm.DB
	var logger = log.Default()
	mysqlConfig := config.AllConfig.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlConfig.User,
		mysqlConfig.Password,
		mysqlConfig.Host,
		mysqlConfig.Port,
		mysqlConfig.Database,
	)

	var err error
	mysqlDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		logger.Z.Error("failed to connect mysql database")
	}
	return mysqlDB
}
